package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBCDemo2 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");//mysql 的驱动类写法是固定的
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/bird?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root","root");
        System.out.println("与数据库连接成功");
        //3.通过链接
        Statement statement = connection.createStatement();
//        String sql1 = "INSERT INTO userinfo (username,password,nickname,age) VALUES('张三','123456','阿三','22')";
//        int num = statement.executeUpdate(sql1);
//        if(num>0){
//            System.out.println("数据插入成功" +num);
//        }
//
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入名字");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        String sql1 = "INSERT INTO userinfo (username,password,nickname,age) VALUES('"+username+"','"+password+"','"+nickname+"',"+age+")";
        int num = statement.executeUpdate(sql1);
        if(num>0){
            System.out.println("数据插入成功" +num);
        }
        statement.close();
    }
}
